package com.hack.qiaoqiao;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ChatMsgAdapter extends BaseAdapter{
	public static interface ChatMsgType{
		int COM_MSG = 0;
		int TO_MSG = 1;
	}
	
	private List<ChatMsgEntity> mList;
	private LayoutInflater mInflater;
	private Context mContext;
	public ChatMsgAdapter(Context context,List<ChatMsgEntity> list){
		mContext=context;
		mInflater=LayoutInflater.from(mContext);
		mList=list;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ChatMsgEntity entity = mList.get(position);
		boolean isComMsg = entity.getMsgType();
		ViewHolder viewHolder = null; 
		if (convertView == null)
		{
			if (isComMsg)
			{
				convertView = mInflater.inflate(R.layout.chat_msg_left_item, null);
			}else{
				convertView = mInflater.inflate(R.layout.chat_msg_right_item, null);
			}
			viewHolder = new ViewHolder();
			viewHolder.tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
			viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
			viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
			viewHolder.isComMsg = isComMsg;
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.tvSendTime.setText(entity.getDate());
		viewHolder.tvUserName.setText(entity.getName());
		viewHolder.tvContent.setText(entity.getContent());
		return convertView;
	}
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		ChatMsgEntity entity =mList.get(position);
		if (entity.getMsgType())
		{
			return ChatMsgType.COM_MSG;
		}else{
			return ChatMsgType.TO_MSG;
		}
	}
	static class ViewHolder { 
		public TextView tvSendTime;
		public TextView tvUserName;
		public TextView tvContent;
		public boolean isComMsg = true;
	}

}
